package com.fh.controller.business;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.service.attachment.AttachmentService;
import com.fh.service.building.RecordService;
import com.fh.service.building.contract.ContractService;
import com.fh.service.business.BusinessService;
import com.fh.service.role.ShsRoleService;
import com.fh.util.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

import static com.fh.controller.system.tools.GetDatalayui.getLData;
import static com.fh.controller.system.tools.GetDatalayui.getPData;


/**
 * 类名称：BusinessController
 * 创建人：FH
 * 创建时间：2017-09-19
 */
@Controller
@RequestMapping(value = "/business")
public class BusinessController extends BaseController {

    @Resource(name = "businessService")
    private BusinessService businessService;

    @Resource(name = "attachmentService")
    private AttachmentService attachmentService;
    @Resource(name = "shsRoleService")
    private ShsRoleService roleService;
    @Resource(name = "recordService")
    private RecordService recordService;
    @Resource(name = "contractService")
    private ContractService contractService;

    /**
     * 返回json
     */
    @RequestMapping(value = "/json")
    @ResponseBody
    public Object json(Page page) {
        PageData pd = new PageData();
        Map<String, Object> map = new HashMap<String, Object>();
        List<PageData> varList;
        String datalayui = null;

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd = this.getPageData();
            pd.put("USER_ID", user.getUSER_ID());
            getPData(page, pd);
            varList = businessService.list(page);
            datalayui = getLData(page, varList);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return datalayui;
    }

    /**
     * 商圈列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public PageData list() {
        logBefore(logger, "Business列表");
        PageData res = new PageData();
        PageData pd = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd.put("USER_ID", user.getUSER_ID());
            List<PageData> list = businessService.listAll(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }

    /**
     * 首页展示商圈列表
     */
    @RequestMapping(value = "/listBuss")
    @ResponseBody
    public PageData listBuss() {
        logBefore(logger, "Business列表");
        PageData res = new PageData();
        PageData pd = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd.put("USER_ID", user.getUSER_ID());
            List<PageData> list = businessService.listBuss(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }

    /**
     * 商圈以及商圈下的楼宇列表
     */
    @RequestMapping(value = "/listBusinessBuilding")
    @ResponseBody
    public PageData listBusinessBuilding() {
        logBefore(logger, "BusinessBuilding列表");
        PageData res = new PageData();
        PageData pd = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd.put("USER_ID", user.getUSER_ID());
            List<PageData> list = businessService.listBusinessBuilding(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }


    /**
     * 新增
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public PageData save() {
        logBefore(logger, "新增Business");
        PageData res = new PageData();
        PageData data = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {

            Map map1 = JSONHelper.json2Map(data.toString());
            Map map2 = JSONHelper.json2Map(map1.get("DATA").toString());
            JSONObject BUSINESS = (JSONObject) map2.get("BUSINESS");
            JSONArray ATTACHMENT = (JSONArray) map2.get("ATTACHMENT");
            String businessId = this.get32UUID();

            // 保存商圈信息
            if (BUSINESS != null && !BUSINESS.equals("")) {
                PageData pd = new PageData();
                pd.put("ID", businessId);    //主键
                pd.put("IS_DELETED", 0); //是否删除
                pd.put("SORT", 0); //排序
                pd.put("CREATE_ID", user.getUSER_ID()); //创建时间
                pd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
                pd.put("BUSINESS_NAME", BUSINESS.get("BUSINESS_NAME"));
                pd.put("BUSINESS_AREA", BUSINESS.get("BUSINESS_AREA"));
                pd.put("BUSINESS_ADDRESS", BUSINESS.get("BUSINESS_ADDRESS"));
                pd.put("BUSINESS_LNG", BUSINESS.get("BUSINESS_LNG"));
                pd.put("BUSINESS_LAT", BUSINESS.get("BUSINESS_LAT"));
                pd.put("file", BUSINESS.get("file"));
                pd.put("IS_OPEN", BUSINESS.get("IS_OPEN"));
                pd.put("BUSINESS_INFO", BUSINESS.get("BUSINESS_INFO"));
                businessService.save(pd);

                //保存全局记录
                PageData SystemPd = new PageData();
                SystemPd.put("ID", this.get32UUID());//主键
                SystemPd.put("IS_DEL", 0); //是否删除
                SystemPd.put("SORT", 0); //排序
                SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
                SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
                SystemPd.put("DETAILS","新增商圈");
                SystemPd.put("OPERA_TIME",new DateUtil().getTime());
                SystemPd.put("OPERATOR",user.getNAME());
                SystemPd.put("IP",user.getIP());
                SystemPd.put("OP_PD",pd.toString());
                recordService.SaveSystem(SystemPd);
            }

            //  保存附件信息
            if (ATTACHMENT.size() > 0) {
                for (int i = 0; i < ATTACHMENT.size(); i++) {
                    PageData pd = new PageData();
                    JSONObject jo = (JSONObject) ATTACHMENT.get(i);
                    pd.put("ID", this.get32UUID());    //主键
                    pd.put("OID", businessId);
                    pd.put("NAME", jo.get("name"));
                    pd.put("SIZE", jo.get("size"));
                    pd.put("EXT", jo.get("ext"));
                    pd.put("PATH", jo.get("url"));
                    pd.put("TYPE", "business");
                    pd.put("SORT", i + 1);
                    pd.put("REMARK", "");
                    pd.put("CREATE_TIME", new DateUtil().getTime());
                    pd.put("CREATE_ID", user.getUSER_ID());
                    pd.put("IS_DELETED", 0);
                    attachmentService.saveAttachment(pd);
                }
            }



            res.put("data", "");
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public PageData edit() {
        logBefore(logger, "修改Business");
        PageData res = new PageData();
        PageData data = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            Map map1 = JSONHelper.json2Map(data.toString());
            Map map2 = JSONHelper.json2Map(map1.get("DATA").toString());
            JSONObject BUSINESS = (JSONObject) map2.get("BUSINESS");
            JSONArray ATTACHMENT = (JSONArray) map2.get("ATTACHMENT");
            String businessId = BUSINESS.getString("ID");

            // 保存商圈信息
            if (BUSINESS != null && !BUSINESS.equals("")) {
                PageData pd = new PageData();
                pd.put("ID", businessId);    //主键
                pd.put("BUSINESS_NAME", BUSINESS.get("BUSINESS_NAME"));
                pd.put("BUSINESS_AREA", BUSINESS.get("BUSINESS_AREA"));
                pd.put("BUSINESS_ADDRESS", BUSINESS.get("BUSINESS_ADDRESS"));
                pd.put("BUSINESS_LNG", BUSINESS.get("BUSINESS_LNG"));
                pd.put("BUSINESS_LAT", BUSINESS.get("BUSINESS_LAT"));
                pd.put("file", BUSINESS.get("file"));
                pd.put("IS_OPEN", BUSINESS.get("IS_OPEN"));
                pd.put("BUSINESS_INFO", BUSINESS.get("BUSINESS_INFO"));
                businessService.edit(pd);
                //保存全局记录
                PageData SystemPd = new PageData();
                SystemPd.put("ID", this.get32UUID());//主键
                SystemPd.put("IS_DEL", 0); //是否删除
                SystemPd.put("SORT", 0); //排序
                SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
                SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
                SystemPd.put("DETAILS","修改商圈");
                SystemPd.put("OPERA_TIME",new DateUtil().getTime());
                SystemPd.put("OPERATOR",user.getNAME());
                SystemPd.put("IP",user.getIP());
                SystemPd.put("OP_PD",pd.toString());
                recordService.SaveSystem(SystemPd);

            }

            PageData p = new PageData();
            p.put("ID", businessId);
            attachmentService.deleteAttachmentByOid(p);
            //  保存附件信息
            if (ATTACHMENT.size() > 0) {
                for (int i = 0; i < ATTACHMENT.size(); i++) {
                    PageData pd = new PageData();
                    JSONObject jo = (JSONObject) ATTACHMENT.get(i);
                    pd.put("ID", this.get32UUID());    //主键
                    pd.put("OID", businessId);
                    pd.put("NAME", jo.get("name"));
                    pd.put("SIZE", jo.get("size"));
                    pd.put("EXT", jo.get("ext"));
                    pd.put("PATH", jo.get("url"));
                    pd.put("TYPE", "business");
                    pd.put("SORT", i + 1);
                    pd.put("REMARK", "");
                    pd.put("CREATE_TIME", new DateUtil().getTime());
                    pd.put("CREATE_ID", user.getUSER_ID());
                    pd.put("IS_DELETED", 0);
                    attachmentService.saveAttachment(pd);
                }
            }

            res.put("data", "");
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }


    /**
     * 修改开启
     */
    @RequestMapping(value = "/editOpenDelete")
    @ResponseBody
    public PageData editOpenDelete() {
        logBefore(logger, "修改Business开启或删除");
        PageData res = new PageData();
        PageData pd = this.getPageData();
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            // 修改商圈信息
            businessService.edit(pd);
            //保存全局记录
            PageData SystemPd = new PageData();
            SystemPd.put("ID", this.get32UUID());//主键
            SystemPd.put("IS_DEL", 0); //是否删除
            SystemPd.put("SORT", 0); //排序
            SystemPd.put("CREATE_ID", user.getUSER_ID()); //创建用户
            SystemPd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间
            SystemPd.put("DETAILS","修改商圈");
            SystemPd.put("OPERA_TIME",new DateUtil().getTime());
            SystemPd.put("OPERATOR",user.getNAME());
            SystemPd.put("IP",user.getIP());
            SystemPd.put("OP_PD",pd.toString());
            recordService.SaveSystem(SystemPd);
            res.put("data", "");
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }

    /**
     * 通过ID获取详情信息
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public PageData info() {
        logBefore(logger, "Business详情");
        PageData res = new PageData();
        PageData pd = this.getPageData();

        try {
            PageData data = businessService.findById(pd);
            pd.put("id", pd.get("ID"));
            List<PageData> attachmentData = attachmentService.findByOId(pd);
            res.put("data", data);
            res.put("attachmentData", attachmentData);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }
    /* ===============================app接口================================== */


    /**
     * 首页商圈列表
     */
   @RequestMapping(value = "/listBussApp",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public Object listBussApp(Page page,HttpServletRequest request) {
        PageData pd = new PageData();
        PageData pds = new PageData();
        PageData listLNG_LATs = new PageData();
        PageData findByOId = new PageData();
        Map<String, Object> map = new HashMap<String, Object>();
        List<PageData> varList;
        String datalayui = null;
       //获取当前登录用户
       Subject currentUser = SecurityUtils.getSubject();
       Session session = currentUser.getSession();
       User user = (User) session.getAttribute(Const.SESSION_USER);
        try {
            pd = this.getPageData();
            pds.put("page",pd.get("page"));
            pds.put("limit",pd.get("row"));
            if(null != pd.get("keywords")){//向pds里面添加参数
                pds.put("keywords",pd.get("keywords") );
                findByOId.put("keywords",pd.get("keywords") );
            }
            //==============================================经纬度
            //HashMap<String, String> BUSINESS_IDS = new HashMap<String, String>();//如果多个商圈的距离相同可以保存到此进行查询
            String BUSINESS_ID = "";//单个距离最大可以用
            String lat1Str;//设置经度
            String lng1Str;//设置维度
            if (null == pd.get("LNG") || "".equals(pd.get("LNG"))) {//判断是否传参（经纬度）
            } else {
                lat1Str = pd.get("LNG").toString();//获取经度
                lng1Str = pd.get("LAT").toString();//获取维度
                List<PageData> listLNG_LAT = businessService.listLNG_LAT(listLNG_LATs);//查询全部商圈的经纬度
                for (int i = 0; i < listLNG_LAT.size() - 1; i++) {//遍历经纬度找到离目标最近的一个商圈
                    String one = MapDistance.getDistance(lat1Str, lng1Str, listLNG_LAT.get(i).get("BUSINESS_LNG").toString(), listLNG_LAT.get(i).get("BUSINESS_LAT").toString());
                    String two = MapDistance.getDistance(lat1Str, lng1Str, listLNG_LAT.get(i + 1).get("BUSINESS_LNG").toString(), listLNG_LAT.get(i + 1).get("BUSINESS_LAT").toString());
                    if ("0.0".equals(one)) {
                        BUSINESS_ID = listLNG_LAT.get(i).get("ID").toString();
                        break;
                    }
                    if ("0.0".equals(two)) {
                        BUSINESS_ID = listLNG_LAT.get(i + 1).get("ID").toString();
                        break;
                    }
                    if (Double.parseDouble(two) - Double.parseDouble(one) < 0) {
                        BUSINESS_ID = listLNG_LAT.get(i).get("ID").toString();
                    }
                    if (Double.parseDouble(two) - Double.parseDouble(one) > 0) {
                        BUSINESS_ID = listLNG_LAT.get(i + 1).get("ID").toString();
                    }
                }
            }
            if (!BUSINESS_ID.equals("") && null != BUSINESS_ID ) {//判断不为空，说明经纬度都有参数
                pds.put("ID", BUSINESS_ID);
            }
            //========================================================================
            pds.put("USER_ID", user.getUSER_ID());
            getPData(page, pds);
            List<PageData> list= new ArrayList<>();
            //判断 当前登录用户 有没有 商圈的权限 APP 暂时没有返回id
            if(null!=pd.get("userid")){
                pd.put("USER_ID", pd.get("userid"));
                PageData dataAuth = roleService.authListByLoginUser(pd);
                if(null!=dataAuth){
                    if(dataAuth.get("AUTH_CODE").toString().indexOf("SQGL_SQCK")!=-1) {
                        list = businessService.listBussApp(page);
                        //计算商圈下的房间数量
//                        count_room(pds, list);
                    }
                }
            }else {
                 list = businessService.listBussApp(page);
//                 count_room(pds, list);
            }
            //获取域名地址 用于组装图片全路径
            StringBuffer url = request.getRequestURL();
            String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
            for (int i = 0; i < list.size(); i++) {
                PageData att = new PageData();
                att.put("id", list.get(i).get("BUSINESS_ID"));
                List<PageData> attachmentData = attachmentService.findByOId(att);//查看附件
                if (attachmentData.size() > 0) {
                    list.get(i).put("IMG", tempContextUrl + attachmentData.get(0).get("PATH"));
                }
                findByOId.put("ID", list.get(i).get("BUSINESS_ID"));
                List<PageData> put = businessService.listBussput(findByOId);//支出
                put.remove(null);
                if (put.size() > 0) {
                    list.get(i).put("put", put.get(0).get("PRICE"));//支出金额计算
                } else {
                    list.get(i).put("put", 0);//支出金额为0 计算
                }
                List<PageData> water = businessService.listBusswater(findByOId);//水费
                water.remove(null);
                if (water.size() > 0) {
                    list.get(i).put("water", water.get(0).get("PRICE"));
                } else {
                    list.get(i).put("water", 0);
                }
                List<PageData> electric = businessService.listBusselectric(findByOId);//电费
                electric.remove(null);
                if (electric.size() > 0) {
                    list.get(i).put("electric", electric.get(0).get("PRICE"));
                } else {
                    list.get(i).put("electric", 0);
                }
            }
            varList = list;
          datalayui = getLData(page, varList);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return datalayui;
    }
    //计算商圈下的 房间数
    private void count_room(PageData pds, List<PageData> list) throws Exception {
        for (int i=0;i<list.size();i++){
            PageData roomnum =new PageData();
            roomnum.put("BUSINESS_ID",list.get(i).get("BUSINESS_ID"));
            List<PageData> roomNum = businessService.RoomNumber(roomnum);
            int room_flag=0;
            for(int k=0;k<roomNum.size();k++){
                PageData roomed = new PageData();
                pds.put("ROOM_ID", roomNum.get(k).get("ROOM_ID"));
                List<PageData> list_roomed = contractService.listAll(pds);//通过房间id 获取合同基本信息
                if(list_roomed.size()>0){
                    room_flag++;
                }
            }
            list.get(i).put("roomnums",roomNum.size());
            list.get(i).put("roomednums",room_flag);
        }
    }


    /**
     * 城市列表
     */
    /*@RequestMapping(value = "/listBussApp")
    @ResponseBody
    public PageData listBussApp() {
        logBefore(logger, "城市列表");
        PageData res = new PageData();
        PageData pd = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            List<PageData> list = businessService.listBussApp(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }*/


    /**
     * 首页商圈列表
     */
/*    @RequestMapping(value = "/listBussApp")
    @ResponseBody
    public PageData listBussApp(HttpServletRequest request) {
        logBefore(logger, "Business列表");
        PageData res = new PageData();
       PageData ress = new PageData();
        PageData listLNG_LATs = new PageData();
        PageData pd = this.getPageData();
        try {
            //================================经纬度

            HashMap<String, String> BUSINESS_IDS = new HashMap<String, String>();//如果多个商圈的距离相同可以保存到此病进行查询
            String BUSINESS_ID = "";//单个距离最大可以用
            String lat1Str;//设置经度
            String lng1Str;//设置维度
            if (null == pd.get("LNG") || "".equals(pd.get("LNG"))) {//判断是否传参（经纬度）
            } else {
                lat1Str = pd.get("LNG").toString();//获取经度
                lng1Str = pd.get("LAT").toString();//获取维度
                List<PageData> listLNG_LAT = businessService.listLNG_LAT(listLNG_LATs);//查询全部商圈的经纬度
                for (int i = 0; i < listLNG_LAT.size() - 1; i++) {//遍历经纬度找到离目标最近的一个商圈
                    String one = MapDistance.getDistance(lat1Str, lng1Str, listLNG_LAT.get(i).get("BUSINESS_LNG").toString(), listLNG_LAT.get(i).get("BUSINESS_LAT").toString());
                    String two = MapDistance.getDistance(lat1Str, lng1Str, listLNG_LAT.get(i + 1).get("BUSINESS_LNG").toString(), listLNG_LAT.get(i + 1).get("BUSINESS_LAT").toString());
                    if ("0.0".equals(one)) {
                        BUSINESS_ID = listLNG_LAT.get(i).get("ID").toString();
                        break;
                    }
                    if ("0.0".equals(two)) {
                        BUSINESS_ID = listLNG_LAT.get(i + 1).get("ID").toString();
                        break;
                    }
                    if (Double.parseDouble(two) - Double.parseDouble(one) < 0) {
                        BUSINESS_ID = listLNG_LAT.get(i).get("ID").toString();
                    }
                    if (Double.parseDouble(two) - Double.parseDouble(one) > 0) {
                        BUSINESS_ID = listLNG_LAT.get(i + 1).get("ID").toString();
                    }
                }
            }
            if (!BUSINESS_ID.equals("")) {//判断不为空，说明经纬度都有参数
                pd.put("ID", BUSINESS_ID);
            }

            //========================================================================

            List<PageData> list = businessService.listBussApp(pd);
            //获取域名地址 用于组装图片全路径
            StringBuffer url = request.getRequestURL();
            String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
            for (int i = 0; i < list.size(); i++) {
                PageData att = new PageData();
                att.put("id", list.get(i).get("BUSINESS_ID"));
                List<PageData> attachmentData = attachmentService.findByOId(att);//查看附件
                if (attachmentData.size() > 0) {
                    list.get(i).put("IMG", tempContextUrl + "ceshi/" + attachmentData.get(0).get("PATH"));
                }
                pd.put("ID", list.get(i).get("BUSINESS_ID"));
                List<PageData> put = businessService.listBussput(pd);//支出
                put.remove(null);
                if (put.size() > 0) {
                    list.get(i).put("put", put.get(0).get("PRICE"));//支出金额计算
                } else {
                    list.get(i).put("put", 0);//支出金额为0 计算
                }
                List<PageData> water = businessService.listBusswater(pd);//水费
                water.remove(null);
                if (water.size() > 0) {
                    list.get(i).put("water", water.get(0).get("PRICE"));
                } else {
                    list.get(i).put("water", 0);
                }
                List<PageData> electric = businessService.listBusselectric(pd);//电费
                electric.remove(null);
                if (electric.size() > 0) {
                    list.get(i).put("electric", electric.get(0).get("PRICE"));
                } else {
                    list.get(i).put("electric", 0);
                }
            }
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }*/


    /**
     * app 商圈列表
     */
    @RequestMapping(value = "/tradlist")
    @ResponseBody
    public PageData tradlist() {
        logBefore(logger, "Business列表");
        PageData res = new PageData();
        PageData pd = this.getPageData();

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd.put("USER_ID", user.getUSER_ID());
            List<PageData> list = businessService.tradlist(pd);
            res.put("data", list);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }


    /**
     * 导出到excel
     *
     * @return
     */
    @RequestMapping(value = "/excel")
    public ModelAndView exportExcel() {
        logBefore(logger, "导出Business到excel");
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<String> titles = new ArrayList<String>();
            titles.add("名称");
            titles.add("面积");
            titles.add("位置");
            titles.add("经度");
            titles.add("纬度");
            titles.add("备注");
            titles.add("创建人");
            titles.add("创建时间");
            dataMap.put("titles", titles);
            pd.put("USER_ID", user.getUSER_ID());
            List<PageData> varOList = businessService.listAll(pd);
            List<PageData> varList = new ArrayList<PageData>();
            for (int i = 0; i < varOList.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", varOList.get(i).getString("BUSINESS_NAME"));
                vpd.put("var2", varOList.get(i).getString("BUSINESS_AREA"));
                vpd.put("var3", varOList.get(i).getString("BUSINESS_ADDRESS"));
                vpd.put("var4", varOList.get(i).getString("BUSINESS_LNG"));
                vpd.put("var5", varOList.get(i).getString("BUSINESS_LAT"));
                vpd.put("var6", varOList.get(i).getString("BUSINESS_INFO"));
                vpd.put("var7", varOList.get(i).getString("CREATE_NAME"));
                vpd.put("var8", varOList.get(i).getString("CREATE_DATE"));
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return mv;
    }

    /* ===============================权限================================== */
    public Map<String, String> getHC() {
        Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
        Session session = currentUser.getSession();
        return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
    }
    /* ===============================权限================================== */

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
    }
}
